import axios from "axios";
import { message } from "ant-design-vue";
import { getToken } from "@/config";
//spring创建请求配置
const spring = axios.create({
    baseURL: '/spring',
    timeout: 5000,
    responseType: 'json',
    headers:{
        'Content-Type': 'application/json;charset=UTF-8',
    }
})
//请求拦截器
spring.interceptors.request.use(
    (res) => {
        //操作res，注入token数据
        //1.获取token
        const token:string|null = getToken()
        if(token){
            res.headers.Authorization = `Bearer\xa0${token}`
        }
        return res
    },
    (err) => {
        //请求失败
        message.destroy()
        message.error(err.message)
        return Promise.reject(err)
    }
)
//响应拦截器
spring.interceptors.response.use(
    (response)=>{
        //响应成功，响应体设置
        return response.data
    },
    (error)=>{
        //响应识别
        message.destroy()
        message.error(error.message)
        return Promise.reject(error)
    }
)

//导出
export default spring
